package com.gexin.service.memberAcitivty;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.ArithUtil;
import com.gexin.common.utils.UUIDUtils;
import com.gexin.model.activity.*;
import com.gexin.model.enums.MemberInfoEnum;
import com.gexin.model.enums.MemberOrderItergralStatusEnum;
import com.gexin.model.order.Order;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


public class InviterService {

    private static final InviteeRelative dao = new InviteeRelative();

    private static List<MemberIntegralLog> memberIntegralLogList = new ArrayList<>();

    private static List<MemberInfo> memberFankeInfoList = new ArrayList<>();

    private static List<InviteeRelative> inviteeRelativeList = new ArrayList<>();

    private static Map<String, MemberIntegralLog> memberIntegralLogMap = new HashMap<>();

    private static List<GroupRule> groupRuleListOrder = new ArrayList<>();

    private static List<GroupRule> groupRuleListOnline = new ArrayList<>();

    private static Map integralMap = new HashMap();

    private static List<MemberInfo> fansAndTaskMemberInfo = new ArrayList<>();

    private static List<GroupRule> groupRuleList = new ArrayList<>();


    private static List<GroupRule> groupRuleOnlines = new ArrayList<>();

    private MemberActivityService memberActivityService = new MemberActivityService();


    /**
     * 递归所有数据
     */
    public void groupByPoint() {

//      查询当前用户对应的inviteerelative(邀请人关系表),以及邀请人列表中当前用户
        this.inviteeRelativeList = InviteeRelative.dao.findAll();

//      获取到所有会员(粉客,推广者),昨天用户所有日志,以及转换成map(根据userId),所有是会员的并且签到过的
        List<MemberInfo> fansInfoList = MemberInfo.dao.memberFankeInfoList(MemberInfoEnum.SECOND_LEVEL.getCode(),
                MemberInfoEnum.THIRD_LEVEL.getCode());
        List<MemberIntegralLog> memberYesterDayIntegralLogList = MemberIntegralLog.dao.findYesterDayLog();
        Map<String, MemberIntegralLog> memberYesterDayIntegralLogMap = memberYesterDayIntegralLogList.stream().collect(Collectors.toMap(
                MemberIntegralLog::getUserId, a -> a, (k1, k2) -> k1));
        this.fansAndTaskMemberInfo = fansInfoList.stream().filter(memberFanke -> memberFanke.getUserId()
                != null && memberYesterDayIntegralLogMap.containsKey(memberFanke.getUserId())).collect(Collectors.toList());


//      获取所有规则
        List<GroupRule> groupRuleList = GroupRule.dao.findAll();
        this.groupRuleListOnline = groupRuleList.stream().filter(groupRule -> new Integer("2").
                equals(groupRule.getType())).collect(Collectors.toList());
        this.groupRuleListOrder = groupRuleList.stream().filter(groupRule -> new Integer("1").
                equals(groupRule.getType())).collect(Collectors.toList());

        for(int i=0;i<fansAndTaskMemberInfo.size();i++){
            MemberInfo memberInfo = fansAndTaskMemberInfo.get(i);
            String userId = memberInfo.getUserId();
            if(userId!=null){
                Optional<InviteeRelative> currentUserInviteeRelative = inviteeRelativeList.
                        stream().filter(currentInviteeRelative -> userId.equals
                        (currentInviteeRelative.getUserId())).findFirst();
                if(currentUserInviteeRelative.isPresent()){
                    InviteeRelative currentInviteeRelative =currentUserInviteeRelative.get();
                    if(currentInviteeRelative.getSelfSum()==null){
                        InviteeRelative queryInviter = currentInviteeRelative;
                        currentInviteeRelative = createTreeForListSetChildren(
                                inviteeRelativeList, currentInviteeRelative, queryInviter);
                    }

                    additionalOrder(userId, currentInviteeRelative.getSelfSum()==null?BigDecimal.ZERO:
                            currentInviteeRelative.getSelfSum());

                }
            }
        }
    }

    /**
     * 补单功能
     * @param userId
     * @param sum
     */
    public void additionalOrder(String userId,BigDecimal sum){
 
        List<Order> orderList = Order.dao.findAll();
        List<MemberOrder> memberOrderList = MemberOrder.dao.findAll();
//          找到当前用户所有兑货券(确认收货和已经开始获得积分的)
        List<Order> currentUserOrderList = orderList.stream().filter(order -> order.getUserId() != null && userId.
                equals(order.getUserId() + "") && new Integer("3").equals(order.getState())).collect(Collectors.toList());
        Map<String, Order> orderMap = currentUserOrderList.stream().filter(Order -> Order.getId() != null).collect(
                Collectors.toMap(Order::getId, a -> a, (k1, k2) -> k1));
        List<MemberOrder> memberOrderListByConfirimRevice = memberOrderList.stream().filter(memberOrder ->
                memberOrder.getStatus() != null && memberOrder.getStatus().equals(MemberOrderItergralStatusEnum.
                        CONFIRM_THE_RECEIVE.getCode())).collect(Collectors.toList());
        List<MemberOrder> currentUserMemberOrderByConfirimRevice = memberOrderListByConfirimRevice.stream().filter(memberOrder
                -> memberOrder.getOrderId() != null && orderMap.get(memberOrder.getOrderId()) != null).collect(
                Collectors.toList());


//      已经获得积分的账单
        Optional<MemberOrder> memberOrderListByGetIntegral = memberOrderList.stream().filter(memberOrder ->
                memberOrder.getStatus() != null && memberOrder.getStatus().equals(MemberOrderItergralStatusEnum.
                        GET_INTEGRAL.getCode()) && orderMap.get(memberOrder.getOrderId()) != null).findFirst();

        MemberActivityService memberActivityService = new MemberActivityService();
        BigDecimal topOff = memberActivityService.getGarde(userId);
        BigDecimal freezeIntegral = BigDecimal.ZERO;
        MemberOrder memberOrder = null;
        if (memberOrderListByGetIntegral.isPresent()) {
            memberOrder = memberOrderListByGetIntegral.get();
//                remainIntegral = memberOrder.getRemainIntegral();
        } else {
            if (CollectionUtils.isNotEmpty(currentUserMemberOrderByConfirimRevice)) {
                memberOrder = currentUserMemberOrderByConfirimRevice.get(0);
                memberOrder.setStatus(MemberOrderItergralStatusEnum.GET_INTEGRAL.getCode());
                memberOrder.update();
            }
        }
        if(memberOrder==null){
            freezeIntegral = sum;
        }else{
            freezeIntegral = memberActivityService.recurrentTask(topOff, memberOrder, sum, currentUserMemberOrderByConfirimRevice);
        }
        MemberBillSpeedIntegral memberBillSpeedIntegral = MemberBillSpeedIntegral.dao.getMemberBillSpeedIntegralByUserId(userId);
        if (memberBillSpeedIntegral == null) {
            memberBillSpeedIntegral = new MemberBillSpeedIntegral();
            memberBillSpeedIntegral.setUserIdAndSpeedIntegralAndCoinAndBillCouponAndForecastAndRemainAndReverseAndDailyIncrement(
                    userId, sum, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO
                    , BigDecimal.ZERO);
            memberBillSpeedIntegral.setFreezeIntegral(freezeIntegral);
            memberBillSpeedIntegral.save();
        }
//          总积分表,freezeIntegral暂存(没有补单的积分)
        BigDecimal speedIntegral = memberBillSpeedIntegral.getSpeedIntegral() == null ?
                BigDecimal.ZERO : memberBillSpeedIntegral.getSpeedIntegral();
        memberBillSpeedIntegral.setSpeedIntegral(speedIntegral.add(sum));

        memberBillSpeedIntegral.setDailyIncrement(sum);
        freezeIntegral = memberBillSpeedIntegral.getFreezeIntegral() == null ?
                freezeIntegral : memberBillSpeedIntegral.getFreezeIntegral();
        freezeIntegral = freezeIntegral.add(sum);

        memberBillSpeedIntegral.setFreezeIntegral(freezeIntegral);
        memberBillSpeedIntegral.setDailyIncrement(sum);

        BigDecimal acquireCoupon = memberBillSpeedIntegral.getAcquireCoupon() == null ?
                BigDecimal.ZERO : memberBillSpeedIntegral.getAcquireCoupon();
        acquireCoupon = acquireCoupon.add(sum);
        memberBillSpeedIntegral.setAcquireCoupon(acquireCoupon);

        BigDecimal remainSpeedIntegral = memberBillSpeedIntegral.getRemainSpeedIntegral() == null ?
                BigDecimal.ZERO : memberBillSpeedIntegral.getRemainSpeedIntegral();
        memberBillSpeedIntegral.setRemainSpeedIntegral(remainSpeedIntegral.subtract(sum));

        memberBillSpeedIntegral.setUpdateTime(new Date().getTime());
        memberBillSpeedIntegral.update();
//          插入日志
        MemberIntegralLog memberIntegralLog = new MemberIntegralLog();
        memberIntegralLog.setUserIdAndintegralAndIncrementIntegralAndStatusAndCommissionAndReserveAndDesc(
                userId, 2, sum, sum, true, BigDecimal.ZERO, BigDecimal.ZERO, "直荐获得积分");
        memberIntegralLog.save();
     }


    /**
     * 拼单,递归查询所有节点的值(没有算上所有的)
     *
     * @param
     * @param inviteeRelativeList
     * @param inviter
     * @return
     */
    public InviteeRelative createTreeForListSetChildren(List<InviteeRelative> inviteeRelativeList,
                                      InviteeRelative inviter,InviteeRelative queryInviter) {

//      按照邀请人分组
        Map<String, List<InviteeRelative>> userGroupMap = inviteeRelativeList.stream().
                filter(inviteeRelative -> inviteeRelative.getInviterId() != null).collect(Collectors.
                groupingBy(InviteeRelative::getInviterId));
        List<InviteeRelative> inviteeRelativeParent = userGroupMap.get(inviter.getId());
        if (inviteeRelativeParent != null) {
            BigDecimal innerSum = BigDecimal.ZERO;
            for (int i = 0; i < inviteeRelativeParent.size(); i++) {
                InviteeRelative inviteeRelative = inviteeRelativeParent.get(i);
                inviter.setChildrenInviteeRelative(inviteeRelativeParent);
                createTreeForListSetChildren(inviteeRelativeList, inviteeRelative,queryInviter);
//              记录当前节点的级别以及(完全可以直接取出所有积分,清零操作) InviteeRelative treeForListSetChildren =
                Optional<MemberInfo> optional = fansAndTaskMemberInfo.stream().filter(fansIntegralLog ->
                        fansIntegralLog.getUserId() != null && fansIntegralLog.getUserId().equals(
                                inviteeRelative.getUserId())).findFirst();
                if (!optional.isPresent()) {
                    innerSum = BigDecimal.ZERO;
                } else {
                    InviteeRelative currentInviter = getGrade(inviteeRelativeList, inviteeRelative);
                     System.out.println(inviteeRelative.getId() + "---------------------" + inviteeRelative.getInviterId());
                }
            }
        }
        if(inviter.getId().equals(queryInviter.getId())){
            getGrade(inviteeRelativeList,inviter);
        }
        return inviter;
    }



    /**
     * 求本级的级别
     */
    public Map currentGrade(InviteeRelative currentInviteeRelative) {

        List<InviteeRelative> inviteeRelativeRecommondList = inviteeRelativeList.stream().filter(
                inviteeRelative -> currentInviteeRelative.getId() != null && currentInviteeRelative.
                        getId().equals(inviteeRelative.getInviterId())).collect(Collectors.toList());
        List<InviteeRelative> inviteeRelativePrototypeList = inviteeRelativeList.stream().filter(
                inviteeRelative -> inviteeRelative.getPrototype() != null && inviteeRelative.
                        getPrototype().contains(currentInviteeRelative.getId())).collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(groupRuleListOrder)) {
            Optional<GroupRule> optional = groupRuleListOnline.stream().filter(groupRule -> groupRule.
                    getNumberOfPeople() != null && groupRule.getNumberOfPeopleMax() != null &&
                    groupRule.getNumberOfPeople() <= inviteeRelativePrototypeList.size() && groupRule.
                    getNumberOfPeopleMax() >= inviteeRelativePrototypeList.size()&&
                     inviteeRelativeRecommondList.size()>=groupRule.getRecommond()).findFirst();
            if (optional.isPresent()) {
                Map map = new HashMap();
                GroupRule groupRule = optional.get();
                map.put("groupRule", groupRule);
                map.put("size", inviteeRelativeRecommondList.size());
                return map;
            }
        }
        return null;
    }


    /**
     * 求和和下一级
     *
     * @param inviteeRelativeList
     * @param inviter
     * @return
     */
    public InviteeRelative getGrade(List<InviteeRelative> inviteeRelativeList, InviteeRelative inviter) {

//      找到自己的级别以及值荐人数(要判断当前人有没有下级,做拼团拼单)
        int currentGroupRuleLevel = 0;
        int currentPeopleOfNumber = 0;
        Map map = currentGrade(inviter);
        BigDecimal balance = BigDecimal.ZERO;
        GroupRule groupRule = null;
        Integer level = 0;
        if (map != null) {
            groupRule = (GroupRule) map.get("groupRule");
            if (groupRule == null) {
                return inviter;
            }
            currentGroupRuleLevel = groupRule.getLevel() == null ? 0 : groupRule.getLevel();
            int number = groupRule.getBalance() == null ? 0 : groupRule.getBalance();
            balance = BigDecimal.valueOf(number);
            currentPeopleOfNumber = (int) map.get("size");

//          他下级有下级的收益
            List<InviteeRelative> inviteeRelativeRecommondList = inviteeRelativeList.stream().filter(
                    inviteeRelative -> inviter.getId() != null && inviter.getId().equals(inviteeRelative.
                            getInviterId()) && inviteeRelative.getTopRecommond()!=null&&!inviteeRelative.getTopRecommond().equals(new BigDecimal("0.00"))
            ).collect(Collectors.toList());
            BigDecimal belowSum = inviteeRelativeRecommondList.stream().filter(inviteeRelative -> inviteeRelative.getTopRecommond()
                    != null).map(InviteeRelative::getTopRecommond).reduce(BigDecimal.ZERO, BigDecimal::add);


            BigDecimal recommond = BigDecimal.ZERO;
            recommond = balance.multiply(new BigDecimal(currentPeopleOfNumber - inviteeRelativeRecommondList.size()));
            BigDecimal groupRuleOrderMulti = BigDecimal.ZERO;
            if (CollectionUtils.isNotEmpty(groupRuleListOrder)) {
                GroupRule groupRuleOrder = groupRuleListOrder.get(0);
                Integer multiple = groupRuleOrder.getMultiple();
                groupRuleOrderMulti = BigDecimal.valueOf(multiple).divide(new BigDecimal("100"));
            }
            recommond = groupRuleOrderMulti.multiply(recommond);
            recommond = recommond.add(balance);
            recommond = recommond.add(belowSum);

            inviter.setSelfSum(recommond);

            recommond = recommond.multiply(groupRuleOrderMulti);
            inviter.setTopRecommond(recommond);
        }

//      和父级的极差
        double differential = getDifferential(inviter, currentGroupRuleLevel);

        inviter.setLevel(currentGroupRuleLevel);
        return inviter;
    }



    public void recurrenceRelativeList(List<InviteeRelative> inviteeRelativeList, List<MemberInfo> fansAndTaskMemberInfo,
                                       List<GroupRule> groupRuleListOrder, List<MemberIntegralLog> memberYesterDayIntegralLogList) {
        GroupRule groupRule = new GroupRule();
        if (CollectionUtils.isNotEmpty(groupRuleListOrder)) {
            groupRule = groupRuleListOrder.get(0);
        } else {
            return;
        }
        List<Order> orderList = Order.dao.findAll();
        List<MemberOrder> memberOrderList = MemberOrder.dao.findAll();

        for (int i = 0; i < inviteeRelativeList.size(); i++) {
            InviteeRelative inviteeRelative = inviteeRelativeList.get(i);
            String inviteeRelativeId = inviteeRelative.getId();
            if (inviteeRelativeId == null) {
                continue;
            }

//          求出当前用户所有参与活动的订单
            String userId = inviteeRelative.getUserId();
            if (StringUtils.isBlank(userId)) {
                continue;
            }

//          所有直系节点,转换成map
            List<InviteeRelative> inviteeRelativeListDict = inviteeRelativeList.stream().filter(inviteeRelativeDict
                    -> inviteeRelativeId.equals(inviteeRelativeDict.getInviterId())).collect(Collectors.toList());
            if (inviteeRelativeListDict.size() < groupRule.getNumberOfPeople()) {
                continue;
            }
            Map<String, InviteeRelative> inviteeRelativeMapDict = inviteeRelativeListDict.stream().filter(inviteeRelativeDict
                    -> inviteeRelativeDict.getUserId() != null).collect(Collectors.toMap(InviteeRelative::getUserId,
                    Function.identity(), (key1, key2) -> key2));

//          是会员的也签到的(直系子节点),并转换成map
            List<MemberInfo> fansAndTaskMemberInfoListDict = fansAndTaskMemberInfo.stream().filter(memberInfo -> memberInfo.
                    getUserId() != null && inviteeRelativeMapDict.get(memberInfo.getUserId()) != null).collect(Collectors.
                    toList());
            if (fansAndTaskMemberInfoListDict.size() < groupRule.getNumberOfPeople()) {
                continue;
            }
            Map<String, MemberInfo> fansAndTaskMemberInfoDictMap = fansAndTaskMemberInfoListDict.stream().filter(
                    fansAndTaskMemberInfoDict -> fansAndTaskMemberInfoDict.getUserId() != null).collect(Collectors.
                    toMap(MemberInfo::getUserId, Function.identity(), (key1, key2) -> key2));

//          拿到直系子节点(是会员的也签到的)的积分
            List<MemberIntegralLog> DictNodeMemberIntegralList = memberYesterDayIntegralLogList.stream().filter(memberIntegralLog
                    -> memberIntegralLog.getUserId() != null && fansAndTaskMemberInfoDictMap.get(memberIntegralLog.getUserId())
                    != null).collect(Collectors.toList());

            BigDecimal sum = DictNodeMemberIntegralList.stream().filter(MemberIntegralLog -> MemberIntegralLog.getIntegral() != null).
                    map(MemberIntegralLog::getIntegralIncrement).reduce(BigDecimal.ZERO, BigDecimal::add);
            int multiple = groupRule.getMultiple() == null ? 0 : groupRule.getMultiple();
            BigDecimal divide = BigDecimal.valueOf(multiple).divide(BigDecimal.valueOf(100));
            sum = sum.multiply(divide);


//          找到当前用户所有兑货券(确认收货和已经开始获得积分的)
            List<Order> currentUserOrderList = orderList.stream().filter(order -> order.getUserId() != null && userId.
                    equals(order.getUserId() + "") && new Integer("3").equals(order.getState())).collect(Collectors.toList());
            Map<String, Order> orderMap = currentUserOrderList.stream().filter(Order -> Order.getId() != null).collect(
                    Collectors.toMap(Order::getId, a -> a, (k1, k2) -> k1));
            List<MemberOrder> memberOrderListByConfirimRevice = memberOrderList.stream().filter(memberOrder ->
                    memberOrder.getStatus() != null && memberOrder.getStatus().equals(MemberOrderItergralStatusEnum.
                            CONFIRM_THE_RECEIVE.getCode())).collect(Collectors.toList());
            List<MemberOrder> currentUserMemberOrderByConfirimRevice = memberOrderListByConfirimRevice.stream().filter(memberOrder
                    -> memberOrder.getOrderId() != null && orderMap.get(memberOrder.getOrderId()) != null).collect(
                    Collectors.toList());


//          已经获得积分的账单
            Optional<MemberOrder> memberOrderListByGetIntegral = memberOrderList.stream().filter(memberOrder ->
                    memberOrder.getStatus() != null && memberOrder.getStatus().equals(MemberOrderItergralStatusEnum.
                            GET_INTEGRAL.getCode()) && orderMap.get(memberOrder.getOrderId()) != null).findFirst();

            MemberActivityService memberActivityService = new MemberActivityService();
            BigDecimal topOff = memberActivityService.getGarde(userId);
            BigDecimal freezeIntegral = BigDecimal.ZERO;
            MemberOrder memberOrder = null;
            if (memberOrderListByGetIntegral.isPresent()) {
                memberOrder = memberOrderListByGetIntegral.get();
//                remainIntegral = memberOrder.getRemainIntegral();
            } else {
                if (CollectionUtils.isNotEmpty(currentUserMemberOrderByConfirimRevice)) {
                    memberOrder = currentUserMemberOrderByConfirimRevice.get(0);
                    memberOrder.setStatus(MemberOrderItergralStatusEnum.GET_INTEGRAL.getCode());
                    memberOrder.update();
                }
            }

            freezeIntegral = memberActivityService.recurrentTask(topOff, memberOrder, sum, currentUserMemberOrderByConfirimRevice);
            MemberBillSpeedIntegral memberBillSpeedIntegral = MemberBillSpeedIntegral.dao.getMemberBillSpeedIntegralByUserId(userId);
            if (memberBillSpeedIntegral == null) {
                memberBillSpeedIntegral = new MemberBillSpeedIntegral();
                memberBillSpeedIntegral.setUserIdAndSpeedIntegralAndCoinAndBillCouponAndForecastAndRemainAndReverseAndDailyIncrement(
                        userId, sum, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO
                        , BigDecimal.ZERO);
                memberBillSpeedIntegral.setFreezeIntegral(freezeIntegral);
                memberBillSpeedIntegral.save();
            }
//          总积分表,freezeIntegral暂存(没有补单的积分)
            BigDecimal speedIntegral = memberBillSpeedIntegral.getSpeedIntegral() == null ?
                    BigDecimal.ZERO : memberBillSpeedIntegral.getSpeedIntegral();
            memberBillSpeedIntegral.setSpeedIntegral(speedIntegral.add(sum));

            memberBillSpeedIntegral.setDailyIncrement(sum);
            freezeIntegral = memberBillSpeedIntegral.getFreezeIntegral() == null ?
                    freezeIntegral : memberBillSpeedIntegral.getFreezeIntegral();
            freezeIntegral = freezeIntegral.add(sum);

            memberBillSpeedIntegral.setFreezeIntegral(freezeIntegral);
            memberBillSpeedIntegral.setDailyIncrement(sum);

            BigDecimal acquireCoupon = memberBillSpeedIntegral.getAcquireCoupon() == null ?
                    BigDecimal.ZERO : memberBillSpeedIntegral.getAcquireCoupon();
            acquireCoupon = acquireCoupon.add(sum);
            memberBillSpeedIntegral.setAcquireCoupon(acquireCoupon);

            BigDecimal remainSpeedIntegral = memberBillSpeedIntegral.getRemainSpeedIntegral() == null ?
                    BigDecimal.ZERO : memberBillSpeedIntegral.getRemainSpeedIntegral();
            memberBillSpeedIntegral.setRemainSpeedIntegral(remainSpeedIntegral.subtract(sum));

            memberBillSpeedIntegral.setUpdateTime(new Date().getTime());
            memberBillSpeedIntegral.update();
//          插入日志
            MemberIntegralLog memberIntegralLog = new MemberIntegralLog();
            memberIntegralLog.setUserIdAndintegralAndIncrementIntegralAndStatusAndCommissionAndReserveAndDesc(
                    userId, 2, sum, sum, true, BigDecimal.ZERO, BigDecimal.ZERO, "直荐获得积分");
            memberIntegralLog.save();
        }
    }


    /**
     * 和父级的极差
     *
     * @param currentInviter
     */
    public double getDifferential(InviteeRelative currentInviter, int currentGroupRuleLevel) {
//      查找父级级别(用来给父类加血)
        if (currentInviter == null) {
            return 0;
        }
        int parentLevel = 0;
//      根据父级id来判别
        String parentInviterId = currentInviter.getInviterId();
        if (parentInviterId == null) {
            return 0;
        }
        Optional<InviteeRelative> inviteParentOperation = inviteeRelativeList.stream().filter(inviteeRelative ->
                inviteeRelative.getId() != null && inviteeRelative.getId().equals(parentInviterId)).findFirst();
        if (!inviteParentOperation.isPresent()) {
            return 0;
        }
        InviteeRelative inviteeRelativeParent = inviteParentOperation.get();
//      拿到父级的级别以及父级的所有人数
        Map map = currentGrade(inviteeRelativeParent);
        if (map != null) {
            GroupRule groupRuleParent = (GroupRule) map.get("groupRule");
            if (groupRuleParent != null) {
                parentLevel = groupRuleParent.getLevel() == null ? 0 : groupRuleParent.getLevel();
//
            }
        }

//      级差（同一等级给30,判断当前用户级别不能0,给30）
        int differential = 0;
        differential = parentLevel - currentGroupRuleLevel;
        if (differential < 0) {
            differential = 0;
        }
        if (differential == 0 && currentGroupRuleLevel == parentLevel && parentLevel != 0) {
            differential = 3;
        }
        int differentialMulti = differential * 10;
        double percent = ArithUtil.percent(differentialMulti, 100);
        return percent;
    }


    /**
     * 获取当前节点所有直推以及所有拼团
     *
     * @param
     */
    //    public GroupRule rule(String userid) {
//        if (CollectionUtils.isNotEmpty(this.groupRuleListOrder)) {
//            GroupRule groupRuleOnline = groupRuleListOrder.get(0);
//            int numberOfPeople = groupRuleOnline.getNumberOfPeople() == null ? 0 : groupRuleOnline.getNumberOfPeople();
////          查询当前用户对应的inviteerelative(邀请人关系表),以及邀请人列表中当前用户
//            Optional<InviteeRelative> inviteeRelativeUser = inviteeRelativeList.stream().filter(inviteeRelative ->
//                    userid.equals(inviteeRelative.getUserId())).findFirst();
//            if (!inviteeRelativeUser.isPresent()) {
//                return null;
//            }
////          找得到他的下级(满足以下条件,签到以及粉丝)
//            InviteeRelative currentUserInviteeRelative = inviteeRelativeUser.get();
//            List<InviteeRelative> linealNode = inviteeRelativeList.stream().filter(inviteeRelative ->
//                    null != inviteeRelative.getInviterId() && inviteeRelative.getInviterId().equals(
//                            currentUserInviteeRelative.getId())).collect(Collectors.toList());
//            InviteeRelative recurrence = null;
//            if (numberOfPeople <= linealNode.size()) {
//                recurrenceRelativeList(inviteeRelativeList);
//            }
//        }
//        return null;
//    }


    //                List<InviteeRelative> inviteeRelativeListOnline = inviteeRelativeList.stream().filter(inviteeRelative ->
//                        inviteeRelative.getPrototype() != null && inviteeRelative.getPrototype().contains(
//                                currentUserInviteeRelative.getId() + "-")).collect(Collectors.toList());
//              拼团
//                Optional<GroupRule> optional = groupRuleListOnline.stream().filter(groupRule -> inviteeRelativeListOnline.size()
//                        >= groupRule.getNumberOfPeople() && groupRule.getNumberOfPeopleMax() > inviteeRelativeListOnline.size()).findFirst();
//                if (!optional.isPresent()) {
//                    return null;
//                }
//                inviteeRelativeOnline(inviteeRelativeUser.get());
//                GroupRule groupRule = optional.get();
//                Integer multiple = groupRule.getMultiple();
//                int i = multiple / 100;
//              依然递归


//     if (inviteParentOperation.isPresent()) {
//          查找父级有多少人也就是级别,查找父级的级别(团下有多少人),先判断直推是否超过两个人,再判断是否大于100人,如果大于多少就是多少的比例
//            InviteeRelative inviteeRelativeParent = inviteParentOperation.get();
//        jsonObject.put("integral", mul);

    //        if(differential<=0){
//            differential=0;
//            return jsonObject;
//        }
//        BigDecimal integralIncrement = BigDecimal.ZERO;
//        integralIncrement = reduce.add(integralIncrement);
//        BigDecimal mul = integralIncrement.multiply(BigDecimal.valueOf(differential));
//        inviter.setTobeSum(integralIncrement);

    //      他的下级(下级和自己的兑货券一起加上去，这样才是给父级的,并且它的下级有没有签到)
/*        List<InviteeRelative> relativeList = inviteeRelativeList.stream().filter(inviteeRelative ->
                inviteeRelative.getInviterId() != null && inviteeRelative.getInviterId().equals(
                        inviter.getId())).collect(Collectors.toList());
        BigDecimal reduce = relativeList.stream().filter(relative -> relative.getSum() != null).map(
                InviteeRelative::getSum).reduce(BigDecimal.ZERO, BigDecimal::add);*/
     /*   Optional<MemberInfo> optional = fansIntegralLogCommon.stream().filter(fansIntegralLog -> fansIntegralLog.
                getUserId() != null && fansIntegralLog.getUserId().equals(inviter.getUserId())).findFirst();
        if (!optional.isPresent()) {
            return jsonObject;
        }else{*/
//          昨天的积分*10
//            MemberInfo memberInfo = optional.get();
//        MemberIntegralLog memberIntegralLog = memberIntegralLogMap.get(memberInfo.getUserId());
//            integralIncrement = memberIntegralLog.getIntegralIncrement() == null ? BigDecimal.ZERO : memberIntegralLog.getIntegralIncrement();
//        }
//        BigDecimal mul = integralIncrement.multiply(BigDecimal.valueOf(percent));

    public void normal(InviteeRelative inviteeRelative) {

//        List<GroupRule> groupRuleList = GroupRule.dao.findAll();
//        groupRuleList.stream().filter(groupRule -> groupRule.getNumberOfPeopleMax()>=inviteeRelative.size()&&
//                groupRule.getNumberOfPeople()<=)
    }
    //                BigDecimal inviteeRelativeSum = inviteeRelative.getSum() == null ? BigDecimal.ZERO : inviteeRelative.getSum();

//            BigDecimal reduce = inviteeRelativeParent.stream().filter(e -> e.getSum() != null).map(e -> e.getSum()).
//                    reduce(BigDecimal.ZERO, BigDecimal::add);
//            inviter.setSum(reduce);
//            integralMap.put(inviter.getId(), inviter);
    //                List<InviteeRelative> collectLeaf = this.inviteeRelativeList.stream().filter(inviteeRelative ->
//                        inviteeRelative.getInviterId()!= null && inviteeRelative.getInviterId().equals(
//                                inviteeRelative.getId())).collect(Collectors.toList());
//              直系子节点(包含签到功能)  多条日志怎么搞,type给6,暂写死
//                List<MemberInfo> dictLineMemberInfoList = memberFankeInfoList.stream().filter(MemberInfo ->
//                        this.memberIntegralLogMap.containsKey(MemberInfo.getUserId())).collect(Collectors.toList());
//                if (numberOfPeople > collectLeaf.size()) {
//                    return;
//                }
    //          拼团规则
//            List<GroupRule> groupRuleOnlineList = groupRuleList.stream().filter(groupRuleOnline -> groupRuleOnline.
//                    getType().equals(2)).collect(Collectors.toList());
//            List<GroupRule> collect1 = groupRuleOnlineList.stream().filter(groupRuleOnline -> groupRuleOnline.
//                    getNumberOfPeople() < inviteeRelativeList.size() && inviteeRelativeList.size() <
//                            groupRuleOnline.getNumberOfPeople()).collect(Collectors.toList());
//            if (CollectionUtils.isEmpty(collect1)) {
//                GroupRule groupRuleOnline = collect1.get(0);
//                int i = groupRuleOnline.getMultiple() == null ? 0 : groupRuleOnline.getMultiple();
//                double percentOnline = ArithUtil.percent(i, 100);
//                Integer balance = groupRuleOnline.getBalance() == null ? 0 : groupRuleOnline.getBalance();
//                double v = percentOnline * inviteeRelativeList.size() * balance;
//            }


//        Optional<InviteeRelative> inviteeRelative = inviteeRelativeList.stream().filter(inviteeRelativeEmun ->
//                inviteeRelativeEmun.getId() != null && inviteeRelativeEmun.getId().equals(inviterId)
//        ).findFirst();
//        if (!inviteeRelative.isPresent()) {
//            return jsonObject;
//        }
//        InviteeRelative inviteeRelativeParent = inviteeRelative.get();
//        String parentUserId =   inviteeRelativeParent.getUserId();
//        MemberIntegralLog memberIntegralLogParent = memberIntegralLogMap.get(parentUserId);
//        if (memberIntegralLogParent == null) {
//            return jsonObject;
//        }
//      GroupRule currentUserGroupRule = GroupRule.dao.range(fansChildren.size());
//      查找当前用户级别
//        MemberInfo memberInfo = MemberInfo.dao.getMemberInfoByUserId(inviter.getUserId());
//        if (memberInfo == null) {
//            return jsonObject;
//        }
//        int level = memberInfo.getLevel() == null ? 0 : memberInfo.getLevel();
//        List<InviteeRelative> collect = inviteeRelativeList.stream().filter(inviteeRelative1 -> inviteeRelative1.getInviterId()
//                != null && inviteeRelative1.getInviterId().contains(inviter.getId())).collect(Collectors.toList());
//        List<InviteeRelative> collect = inviteeRelativeList.stream().filter(inviteeRelative1 -> inviteeRelative1.getInviterId()
//                != null && inviteeRelative1.getInviterId().contains(inviter.getId())).collect(Collectors.toList());
//        List<InviteeRelative> fansChildren = collect.stream().filter(inviteeRelative -> inviteeRelative.getUserId() != null
//                && this.memberIntegralLogMap.containsKey(inviteeRelative.getUserId())).collect(Collectors.toList());
//        GroupRule groupRule = GroupRule.dao.getGroupRule(collect.size() + "");

    public ApiResponse getLeaf(String inviterId) {
        InviteeRelative inviteeRelative = InviteeRelative.dao.getInviteeRelative(inviterId);
        if (inviteeRelative == null || inviteeRelative.getId() == null) {
            return new ApiResponse(false, "该用户没有邀请过任何人");
        }
        String inviteeRelativeId = inviteeRelative.getId();
        List<InviteeRelative> inviteeRelativeList = InviteeRelative.dao.findByInviterId(inviteeRelativeId);
        return new ApiResponse(true, "返回成功", inviteeRelativeList);
    }

    public InviteeRelative getDiscount(WeixinUser authUser, int status) {
        String id = authUser.getId() + "";
        MemberCommisionLog memberCommisionLog = MemberCommisionLog.dao.getMemberCommisionLogById(id, status);
        memberCommisionLog.setId(UUID.randomUUID() + "");
        InviteeRelative inviteeRelative = InviteeRelative.dao.getInviteeRelative(authUser.getId() + "");
        return inviteeRelative;
    }

    public static ApiResponse getInviteCode(String id) {
        InviteeRelative inviteeRelative = InviteeRelative.dao.getInviteeRelative(id);
        String inviteeRelativeId = "";
        if (inviteeRelative == null) {
            inviteeRelative = new InviteeRelative();
            inviteeRelative.setId(System.currentTimeMillis() / 1000 + "");
            inviteeRelative.setUserId(id);
            inviteeRelative.save();
        }
        inviteeRelativeId = inviteeRelative.getId();
        return new ApiResponse(true, "获取邀请码成功", inviteeRelativeId);
    }

    @Before(Tx.class)
    public void test() {
        List<MemberInfo> memberInfoAll = MemberInfo.dao.findAll();
        for (int i = 0; i < memberInfoAll.size(); i++) {
            MemberInfo memberInfo = memberInfoAll.get(i);
            if ((memberInfo.getLevel() != MemberInfoEnum.FIRST_LEVEL.getCode()
                    || memberInfo.getLevel() != MemberInfoEnum.SECOND_LEVEL.getCode())
                    && memberInfo.getUserId() != null) {
                groupByPoint();
            }
        }
    }

    /**
     * 获取推广者所有递归的成员
     */
//    public InviteeRelative getRecurrence(InviteeRelative inviteeRelative, List<InviteeRelative> inviteeRelativeList) {
//        InviteeRelative treeForListSetChildren = createTreeForListSetChildren(BigDecimal.ZERO, inviteeRelativeList, inviteeRelative);
//        if (inviteeRelativeList.size() == 0) {
//            return inviteeRelative;
//        }
//        return inviteeRelative;
//    }

    /**
     * 邀请功能
     */
    public ApiResponse getInviterCode(String inviterId) {
        InviteeRelative inviteeRelativeParent = InviteeRelative.dao.getInviteeRelativeByInteviterId(inviterId);
        if (inviteeRelativeParent == null) {
            return new ApiResponse(false, "邀请人不存在,请检查后重试");
        }
        return new ApiResponse(true, "成功返回");
    }


    /**
     * 邀请功能
     *
     * @param authId
     * @return
     */
    public ApiResponse inviter(String authId, String inviterId) {
        InviteeRelative inviteeRelativeParent = InviteeRelative.dao.getInviteeRelativeByInteviterId(inviterId);
        if (inviteeRelativeParent == null) {
            return new ApiResponse(false, "邀请人不存在,请检查后重试");
        }
        InviteeRelative inviteeRelative = new InviteeRelative();
        inviteeRelative.setId(UUIDUtils.getUUIDInOrderId() + "");
        inviteeRelative.setUserId(authId);
        String prototype = inviteeRelativeParent.getPrototype() == null ? "0" : inviteeRelativeParent.getPrototype();
        String inviteeRelativeId = inviteeRelativeParent.getId();
        inviteeRelative.setPrototype(prototype + "-" + inviteeRelativeId);
        inviteeRelative.setInviterId(inviteeRelativeId);
        long time = new Date().getTime();
        inviteeRelative.setCretateTime(time);
        inviteeRelative.setUpdateTime(time);
        inviteeRelative.save();
        return new ApiResponse(true, "邀请成功");
    }


    /**
     * 获得当前所有邀请人
     *
     * @param authId
     * @return
     */
    public ApiResponse getInviterListByprototype(String authId) {
        InviteeRelative inviteeRelative = dao.getInviteeRelative(authId);
        if (inviteeRelative == null) {
            return new ApiResponse(true, "累计邀请0人", "0");
        }
        List<InviteeRelative> inviteeRelativeList = InviteeRelative.dao.findAll();
        List<InviteeRelative> collect = inviteeRelativeList.stream().filter(inviteeRelative1 -> inviteeRelative1.getInviterId() != null
                && inviteeRelative1.getInviterId().contains(inviteeRelative.getId())).collect(Collectors.toList());
        Map map = new HashMap();
        map.put("peopleOfNum", collect.size());
        map.put("inviteeRelative", collect);
        return new ApiResponse(true, "返回成功", map);
    }

    /**
     * 邀请人任务功能
     */
    public void task(String id, List tree) {
//      邀请人任务功能
        RangeProportion rangeProportion = new RangeProportion();
        List<RangeProportion> rangeProportionList = rangeProportion.dao().findAll();
        for (int i = 0; i < rangeProportionList.size(); i++) {
            rangeProportion = rangeProportionList.get(i);
            rangeProportion.getInviterMemberCount();
        }
    }


    /**
     * 应用商城二维码
     *
     * @return
     */
    public ApiResponse getQrCode() {
        List<AppStoreUrl> appStoreStrList = AppStoreUrl.dao.findAll();
        Optional<AppStoreUrl> first = appStoreStrList.stream().filter(appStoreUrl -> new Integer("1").equals
                (appStoreUrl.getType())).findFirst();
        AppStoreUrl appStore = null;
        if (first.isPresent()) {
            appStore = first.get();
            return new ApiResponse(true, "获取失败", appStore);
        }
        return new ApiResponse(true, "获取成功", appStore);

    }




    /*------------------------------------------------------------------------------------------------*/

    /**
     * 获取推广者所有递归的成员
     */
    public InviteeRelative recurrence(String id) {
        InviteeRelative inviteeRelative = InviteeRelative.dao.getInviteeRelative(id);
        if (null == inviteeRelative) {
            return inviteeRelative;
        }
        List<InviteeRelative> inviteeRelativeList = InviteeRelative.dao.findAll();
//        inviteeRelative = recurrenceRelativeList(inviteeRelative, inviteeRelativeList);
//        InviteeRelative treeForListSetChildren = createTreeForListSetChildren(BigDecimal.ZERO, inviteeRelativeList, inviteeRelative);
        if (inviteeRelativeList.size() == 0) {
            return inviteeRelative;
        }
        return inviteeRelative;
    }


}

//        InviteeRelative treeForListSetChildren = createTreeForListSetChildren( inviteeRelativeList, inviteeRelative);
//        if (inviteeRelativeList.size() == 0) {
//            return inviteeRelative;
//        }
//        return inviteeRelative;

//        if (inviteParentOperation.isPresent()) {
////          查找父级的级别(团下有多少人/10或者100,先判断直推是否超过两个人,再判断是否大于100人,如果大于多少就是多少的比例)
//            InviteeRelative inviteeRelativeParent = inviteParentOperation.get();
//            List<InviteeRelative> inviteeRelativeParentList = inviteeRelativeList.stream().filter(inviteeRelative -> inviteeRelativeParent.getId()
//                    != null && inviteeRelativeParent.getId().equals(inviteeRelative.getInviterId())).collect(Collectors.toList());
//            if (inviteeRelativeParentList.size() >= 2) {
//                List<InviteeRelative> inviteeRelativeListOnline = inviteeRelativeList.stream().filter(inviteeRelative -> inviteeRelative.getId()
//                        != null && inviteeRelative.getPrototype().contains(inviteeRelativeParent.getId())).collect(Collectors.toList());
//                List<GroupRule> groupRuleList = GroupRule.dao.findAll();
//                first = groupRuleList.stream().filter(groupRule
//                        -> inviteeRelativeListOnline.size() < groupRule.getNumberOfPeople()
//                        && groupRule.getNumberOfPeople() >= inviteeRelativeListOnline.size()).findFirst();
//            }
//        }

//      如果下级人数大于2
//        if (CollectionUtils.isNotEmpty(collect)) {
//            GroupRule groupRule = collect.get(0);
//            Integer numberOfPeople = groupRule.getNumberOfPeople() == null ? 0 : groupRule.getNumberOfPeople();
////          找得到他的下级(满足以下条件,1签到,2是粉丝)
//            List<InviteeRelative> collectLeaf = inviteeRelativeList.stream().filter(inviteeRelative ->
//                    inviteeRelative.getPrototype() != null && inviteeRelative.getPrototype().
//                            contains(inviteeRelative.getId())).collect(Collectors.toList());
//            if (numberOfPeople > collectLeaf.size()) {
//                return;
//            }
//            InviteeRelative recurrence = recurrence(userid);
//
////          直系子节点(包含签到功能)  多条日志怎么搞,type给6,暂写死
//            List<MemberInfo> dictLineMemberInfoList = memberFankeInfoList.stream().filter(MemberInfo ->
//                    memberIntegralLogMap.containsKey(MemberInfo.getUserId())).collect(Collectors.toList());
//
//
////          拼团规则
//            List<GroupRule> groupRuleOnlineList = groupRuleList.stream().filter(groupRuleOnline -> groupRuleOnline.getType().equals(2)).collect(Collectors.toList());
//            List<GroupRule> collect1 = groupRuleOnlineList.stream().filter(groupRuleOnline -> groupRuleOnline.getNumberOfPeople() < inviteeRelativeList.size() &&
//                    inviteeRelativeList.size() < groupRuleOnline.getNumberOfPeople()).collect(Collectors.toList());
//            if (CollectionUtils.isEmpty(collect1)) {
//                GroupRule groupRuleOnline = collect1.get(0);
//                int i = groupRuleOnline.getMultiple() == null ? 0 : groupRuleOnline.getMultiple();
//                double percentOnline = ArithUtil.percent(i, 100);
//                Integer balance = groupRuleOnline.getBalance() == null ? 0 : groupRuleOnline.getBalance();
//                double v = percentOnline * inviteeRelativeList.size() * balance;
//            }

//        }


//          会员订单表
//            List<MemberOrderIntegral> orderIntegralList = MemberOrderIntegral.dao.getMemberOrder();
//            List<GroupRule> groupRules = groupRuleList.stream().filter(groupRuleOnline -> groupRuleOnline.getType().equals(2)).collect(Collectors.toList());
//            int sum = 0;
//            List<MemberBillSpeedIntegral> billSpeedIntegralList = MemberBillSpeedIntegral.dao.findAll();
//            int ducthIngtegral = 0;
//            if (dictLineMemberInfoList.size() >= 2) {
//                int dutch = 0;
//                for (int i = 0; i < dictLineMemberInfoList.size(); i++) {
//                    MemberInfo memberInfo = dictLineMemberInfoList.get(i);
//                    String userId = memberInfo.getUserId();
////                  根据会员id查找会员订单
//                    List<MemberBillSpeedIntegral> collectList = billSpeedIntegralList.stream().filter(billSpeedIntegral -> billSpeedIntegral.getUserId() != null &&
//                            billSpeedIntegral.getUserId().equals(userId)).collect(Collectors.toList());
//                    int dailyIncrement = 0;
//                    if (CollectionUtils.isNotEmpty(collectList)) {
//                        MemberBillSpeedIntegral memberBillSpeedIntegral = collectList.get(0);
////                        dailyIncrement = memberBillSpeedIntegral.getDailyIncrement() == null ? 0 : memberBillSpeedIntegral.getDailyIncrement();
//                        dutch += dailyIncrement;
//                    }
//                    double percent = ArithUtil.percent(groupRule.getMultiple(), 100);
//                    ducthIngtegral = (int) (dutch * percent);
//                  找到直属子节点下所有的节点
//                    List<InviteeRelative> InviteeRelativeList = inviteeRelativeList.stream().filter(inviteeRelative -> inviteeRelative.getPrototype().contains(userId + "-")).collect(Collectors.toList());
//                }
//            }

//  获得每天的增值积分(既兑货券)
//    public void getDailyIncrement() {
//        InviteeRelative inviteeRelative = new InviteeRelative();
//        List<InviteeRelative> childrenInviteeRelativeList = inviteeRelative.getChildrenInviteeRelative();
//        List<MemberBillSpeedIntegral> memberBillSpeedIntegral = MemberBillSpeedIntegral.dao.findAll();
//        Map<String, MemberBillSpeedIntegral> map = memberBillSpeedIntegral.stream().collect(Collectors.toMap(MemberBillSpeedIntegral::getUserId, Function.identity()));
//        int levelSecond = 0;
//        getTree(levelSecond,childrenInviteeRelativeList,map);
//
//    }

//    public void getTree(int levelSecond,List<InviteeRelative>  childrenInviteeRelative,Map<String, MemberBillSpeedIntegral> map){
//            for(int i=0;i<childrenInviteeRelative.size();i++){
//                InviteeRelative inviteeRelativeChildren = childrenInviteeRelative.get(i);
////                inviteeRelativeChildren.setBranch(childrenInviteeRelative.get(i));
//                List<InviteeRelative> childrenInviteeRelativeChildrenList = inviteeRelativeChildren.getChildrenInviteeRelative();
////               如果是最后一个,不存在子节点
//                MemberBillSpeedIntegral memberBillSpeedIntegral = map.get(inviteeRelativeChildren.getUserId());
//                int dailyIncrement = 0;
//                if(memberBillSpeedIntegral!=null) {
//                    dailyIncrement = memberBillSpeedIntegral.getDailyIncrement();
//                    inviteeRelativeChildren.setLevel(levelSecond);
//                }
//                if(childrenInviteeRelativeChildrenList.size()==0){
//                    levelSecond += 1;
//                    inviteeRelativeChildren.setLevel(levelSecond);
//                    return ;
//                }else{
////                  2,3,4的情况,取出最后一级所有的增值积分
//                    if(childrenInviteeRelativeChildrenList.size()>=2&&childrenInviteeRelativeChildrenList.size()<=100){
//                        int sum = 0;
//                        for(int j=0;j<childrenInviteeRelativeChildrenList.size();j++){
//                            InviteeRelative inviteeRelative = childrenInviteeRelativeChildrenList.get(j);
//                            MemberBillSpeedIntegral memberBillSpeedIntegralChild = map.get(inviteeRelative.getUserId());
//                            sum += memberBillSpeedIntegralChild.getDailyIncrement();
//                        }
//                        levelSecond += 1;
//                        int v = (int) (sum * 0.25);
//                        inviteeRelativeChildren.setLevel(levelSecond);
//                        childrenInviteeRelativeChildrenList.size();
//                    }
//                    getTree(childrenInviteeRelativeChildrenList,map);
//
//                }
//            }
//
//    }


//    public void afterOrder(InviteeRelative inviteeRelative){
//        if(inviteeRelative==null){
//            return;
//        }
//        afterOrder(inviteeRelative.getInviteeRelative(),inviteeRelative.getInviteeRelative().);
//    }

//   获取推广者父级以及顶级
//    public InviteeRelative getPromotersDiscount(WeixinUser authUser) {
//        Integer id = authUser.getId();
//        List<InviteeRelative> list = new ArrayList<InviteeRelative>();
//        InviteeRelative inviteeRelative = InviteeRelative.dao.findFirst("select * from t_invitee_relative where user_id=" + id);
//        if (null == inviteeRelative) {
//            return null;
//        }
//        inviteeRelative = recurrenceHighRelativeList(inviteeRelative, list);
//        return inviteeRelative;
//    }


//    private InviteeRelative recurrenceHighRelativeList(InviteeRelative currentInviteer, List<InviteeRelative> list) {
//        String inviterId = currentInviteer.getInviterId();
//        if (StringUtils.isBlank(inviterId)) {
//            return null;
//        }
//        List<InviteeRelative> parentInviterList = InviteeRelative.dao.findAll();
//        InviteeRelative treeForList = createTreeForList(currentInviteer, parentInviterList);
//        return treeForList;
//    }


//    private InviteeRelative createTreeForList(InviteeRelative currentInviteer, List<InviteeRelative> parentInviterList) {
//        Map<String, InviteeRelative> map = parentInviterList.stream().collect(Collectors.toMap(InviteeRelative::getId, Function.identity()));
//        InviteeRelative inviteeRelativeParent = map.get(currentInviteer.getInviterId());
//        if (inviteeRelativeParent != null) {
//            inviteeRelativeParent.setChildrenInviteeRelative(Arrays.asList(currentInviteer));
//            createTreeForList(inviteeRelativeParent, parentInviterList);
//            return currentInviteer;
//        } else {
//            return currentInviteer;
//        }
//    }


